package com.zjj.springboot.jdbcTemplate.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.zjj.springboot.jdbcTemplate.bean.User;
import com.zjj.springboot.jdbcTemplate.dao.UserDao;

@Repository
public class UserDaoImpl implements UserDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<User> queryUserList() {
		return jdbcTemplate.query("select * from user",new BeanPropertyRowMapper(User.class));
	}

	@SuppressWarnings("rawtypes")
	@Override
	public User queryUserById(int id) {
		List<User> list = jdbcTemplate.query("select * from user where id = ?", new Object[] { id },
				new BeanPropertyRowMapper(User.class));
		if (list != null && list.size() > 0) {
			User account = list.get(0);
			return account;
		}
		return null;
	}

	@Override
	public boolean addUser(User user) {
		int status = jdbcTemplate.update("insert into user(name, age) values(?, ?)", user.getName(), user.getAge());
		return status >= 1;
	}

	@Override
	public boolean updateUser(User user) {
		int status = jdbcTemplate.update("update user set name=?,age=? where id=?", user.getName(), user.getAge(),
				user.getId());
		return status >= 1;
	}

	@Override
	public boolean deleteUser(int id) {
		int status = jdbcTemplate.update("delete from user where id=?", id);
		return status >= 1;
	}
}
